package pers.jing.android.helper.work

import kotlinx.coroutines.delay
import pers.jing.android.helper.AndroidSdkTools
import pers.jing.android.helper.model.CODE_SUCCESS
import kotlin.time.Duration.Companion.seconds

/**
 *
 */
class DyjsbWork(val androidSdkPath: String, override val id: String): OneTimeWork {

    override suspend fun run() {
        //打开APP
        var cResult = AndroidSdkTools.startApp(androidSdkPath, deviceSerialId = id,
            pkgName = "com.ss.android.ugc.aweme.lite",
            activityName = "com.ss.android.ugc.aweme.splash.SplashActivity")
        if (CODE_SUCCESS != cResult.code) {
            //启动失败
            println("start app error: ${cResult.error?.joinToString("\n")?:"unknown"}")
            return
        }
        //等待1秒
        delay(1.seconds)
        //获取屏幕信息
        var uiHierarchy = AndroidSdkTools.getScreenshotElements(androidSdkPath, deviceSerialId = id)
        if (uiHierarchy == null) {
            println("get dyjsb home root node error")
            return
        }
        //寻找"赚钱"
        var node = AndroidSdkTools.findNodeBy(uiHierarchy, "赚钱")
        if (node == null) {
            println("get 赚钱 node error")
            return
        }
        val zqCenter = node.getCenter()
        if (zqCenter == null) {
            println("get 赚钱 center error")
            return
        }
        //点击赚钱
        cResult = AndroidSdkTools.tap(androidSdkPath, deviceSerialId = id, point = zqCenter)
        if (CODE_SUCCESS != cResult.code) {
            println("click 赚钱 error: ${cResult.error?.joinToString("\n")?:"unknown"}")
            return
        }
        //等待1秒
        delay(1.seconds)
        uiHierarchy = AndroidSdkTools.getScreenshotElements(androidSdkPath, deviceSerialId = id)
        if (uiHierarchy == null) {
            println("get dyjsb zq-page root node error")
            return
        }
        //寻找"去打卡"
        node = AndroidSdkTools.findNodeBy(uiHierarchy, "去打卡")
        if (node == null) {
            //未找到, 再尝试找已打卡
            node = AndroidSdkTools.findNodeBy(uiHierarchy, "已打卡")
            if (node == null) {
                println("get dyjsb zq-page node error: 去打卡和已打卡均未找到")
                return
            }
            println("已打卡")
        } else {
            val qdkCenter = node.getCenter()
            if (qdkCenter == null) {
                println("get 去打卡 center error")
                return
            }
            cResult = AndroidSdkTools.tap(androidSdkPath, deviceSerialId = id, point = qdkCenter)
            if (CODE_SUCCESS != cResult.code) {
                println("click 去打卡 error: ${cResult.error?.joinToString("\n")?:"unknown"}")
                return
            }
            //等待一秒
            delay(1.seconds)
            //寻找"去观看"
            node = AndroidSdkTools.findNodeBy(uiHierarchy, "去观看")
            if (node == null) {
                println("get 去观看 error: ${cResult.error?.joinToString("\n")?:"unknown"}")
                return
            }
            cResult = AndroidSdkTools.tap(androidSdkPath, deviceSerialId = id, point = qdkCenter)
            if (CODE_SUCCESS != cResult.code) {
                println("click 去观看 error: ${cResult.error?.joinToString("\n")?:"unknown"}")
                return
            }
        }
    }
}